
//
//  YMSNetWorkToll.swift
//  Swift网络封装
//
//  Created by 杨茂盛 on 2016/11/14.
//  Copyright © 2016年 杨茂盛. All rights reserved.
//

import UIKit
import AFNetworking
enum HTTPMethod:Int {
    case GET = 0
    case POST
}
class YMSNetWorkToll: AFHTTPSessionManager {
    
    //声明单例对象
    static let shared:YMSNetWorkToll = {
        let tool = YMSNetWorkToll()
        tool.responseSerializer.acceptableContentTypes?.insert("text/html")
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        tool.responseSerializer.acceptableContentTypes?.insert("text/json")
        tool.requestSerializer = AFJSONRequestSerializer();
        return tool
    }()
    func request(method:HTTPMethod,urlString:String,parameters:Any?,finished:@escaping ((Any?,Error?) -> ())){
        let successCallBack = {(task:URLSessionDataTask,response:Any?) -> () in
        finished(response,nil)
            
        }
        let failureCallBack = {(task:URLSessionDataTask?,error:Error) -> () in
            print(error)
            finished(nil,error)
        }
        
        if method == .GET {
            self.get(urlString, parameters: parameters, progress: nil, success: successCallBack, failure: failureCallBack)
        }else{
            self.post(urlString, parameters: parameters, progress: nil, success: successCallBack, failure: failureCallBack)
        }

    }
}
